<?php

namespace App\Common;

/**
 * 短信类
 */
class Sms
{
    private static $max_num = 20;
    private static $conf = [
        'uid' => 'mxt',
        'password' => 'hA2oM3jJ'
    ];

    private static $type = [
        1 => '注册',
        2 => '忘记密码',
        3 => '设置支付密码',
        4 => '绑定支付宝',
        5 => '绑定银行卡',
        6 => '解绑支付宝',
        7 => '解绑银行卡',
        8 => '修改登录密码验证',
        9 => '修改二级交易密码验证',
    ];

    public function reg_send( $phone, $type , $model )
    {
        $num = rand( 100000, 999999 );
        $event_type = self::$type[ $type ];
        switch ( $type ) {
            case 1: //注册
                $msg = "您的注册验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 2:
                //忘记密码
                $msg = "您忘记密码的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 3:
                //设置支付密码
                $msg = "您设置支付密码的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 4:
                //绑定支付宝
                $msg = "您绑定支付宝的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 5:    //绑定银行卡
                $msg = "您绑定银行卡的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 6:    //解绑支付宝
                $msg = "您解绑支付宝的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 7:    //解绑银行卡
                $msg = "您解绑银行卡的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 8:
                //修改登录密码验证
                $msg = "您修改登录密码的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;
            case 9:
                //修改二级交易密码验证
                $msg = "您修改二级交易密码的验证码是{$num}，如非本人操作，请忽略本短信。【明心堂】";
                break;

        }
        $time = [strtotime( date( 'Ymd' ) ), strtotime( date( 'Ymd' ) . '23:59:59' )];

        $sms = $model
            ->where( 'mobile', $phone )
            ->where( 'type', $type )
            ->whereIn( 'createtime', $time )
            ->count( 'sms' );
        $continue = $model
            ->where( 'mobile', $phone )
            ->where( 'type', $type )
            ->where( 'status', 1 )
            ->orderBy( 'id', 'DESC' )
            ->getValue( 'sms', 'createtime' );
        if ( $continue && $continue + 600 > time() ) {
            return ['status' => -2];
        }

        //超过每日类型下的短信条数
        if ( $sms >= self::$max_num ) {
            return ['status' => -1];
        }

        $model->startTransaction();
        $sms_insert = [
            'type' => $type,
            'mobile' => $phone,
            'code' => $num,
            'status' => 1,
            'createtime' => time()
        ];
        $res = $model->insert( 'sms', $sms_insert );
        if ( $res && $msg ) {

            $result = self::send( $phone, $msg );

//            $result = 1;
            if ( $result ) {
                $model->commit();
                return ['status' => $result, 'code' => $num];
            }
            $model->rollback();
            return ['status' => -1];
        } else {
            $model->rollback();
            return ['status' => -1];
        }
    }

    public static function send( $phone, $msg )
    {
        $data = "username=%s&password=%s&mobile=%s&content=%s";
        $url = "http://120.55.248.18/smsSend.do?";
        $name = self::$conf['uid'];
        $pwd = md5( self::$conf['password'] );
        $pass = md5( $name . $pwd );
        $to = $phone;
        $content = $msg;
        $content = urlencode( $content );
        $rdata = sprintf( $data, $name, $pass, $to, $content );
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $rdata );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
    }

    //验证注册验证码
    public function confirmation( $phone, $code = '', $type = 1, $model )
    {

        try {
            if ( !$code ) {
                throw new \Exception( '不存在验证码', -101 );
            }
            if ( $code == '111111' ) {
                return ['status' => 1, 'msg' => '成功'];   //todo  测试时使用
            }
            $time = time();
            $sms = $model
                ->where( 'mobile', $phone )
                ->where( 'type', $type)
                ->where( 'createtime', $time - 600, '>' )
                ->orderBy( 'id', 'desc' )
                ->getOne( 'sms', '*' );
            if ( !$sms ) {
                throw new \Exception( '请重新获取验证码', -102 );
            }
            if ( $sms['status'] > 1 ) {
                throw new \Exception( '已经使用', -103 );
            }
            if ( $sms['code'] != $code ) {
                throw new \Exception( '验证码错误', -104 );
            }
            $model->where( 'id', $sms['id'] )->setInc( 'sms', 'status' );
            return ['status' => 1, 'msg' => '成功'];
        } catch ( \Exception $e ) {
            return ['status' => $e->getCode(), 'msg' => $e->getMessage()];
        }
    }
}